function aptWeekendInit() {

    var userHasRights = false;
    emosWS.addEventListener("UserRight", function(msg) {
        if (AptCheckUserRight("Release")) {
            userHasRights = true;
        } else {
            userHasRights = false;
        }
        userRightsChanged(userHasRights);
    });

    var advisedId = emosWS.getNewElementId();
    var languageTexts = {
        "T02_Weekend": {
            id: "aptWeekendHeader",
            text: "Weekend mode"
        }
    };

    var opcTopic = sessionStorage.getItem("apt_PLC.OpcTopic");
    var weekendState = sessionStorage.getItem("apt_PLC.Tags.WeekendOn");

    function createDom() {

        var content =
            '<span id="releaseWeekendId" class="emosbutton buttonGroup stategrey weekendIcon disabled"><div class="innerImageDiv"><div id = "innerId" class="innerImageDivWeekend"></div></div></span>';

        $('#weekendButtonContainer').append(content);

    }

    function dataHandler() {
        emosWS.advise(opcTopic + weekendState, function(msg) {
            switch (msg.value) {
                case "1":
                    {
                        $('#releaseWeekendId').addClass("stategreen").removeClass("stategrey");
                        break;
                    }
                default:
                    {
                        $('#releaseWeekendId').addClass("stategrey").removeClass("stategreen");
                    }
            }
        }).setId(advisedId);

    }

    function userRightsChanged() {
        if (userHasRights) {
            $('.emosbutton').removeClass("disabled");
        } else {
            $('.emosbutton').addClass("disabled");
        }
    }

    function eventHandler() {
        $('#releaseWeekendId').click(function() {
           emosWS.poke(opcTopic + weekendState, +!GetTagValueAsBoolean(opcTopic + weekendState));
        });
    }

    createDom();
    languageHandler(languageTexts);
    dataHandler();
    eventHandler();
}

function initWeekendWindow() {
    emosWS.connect({ onopen: aptWeekendInit() });
}